3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup Maintenance
22 require_once __DIR__
. '/../Maintenance.php';
25 * Manage foreign resources registered with ResourceLoader.
27 * @ingroup Maintenance
30 class ManageForeignResources
extends Maintenance
{
31 private $defaultAlgo = 'sha384';
32 private $tmpParentDir;
34 private $failAfterOutput = false;
36 public function __construct() {
38 parent
::__construct();
39 $this->addDescription( <<<TEXT
40 Manage foreign resources registered with ResourceLoader.
42 This helps developers to download, verify and update local copies of upstream
43 libraries registered as ResourceLoader modules. See also foreign-resources.yaml.
45 For sources that don't publish an integrity hash, omit "integrity" (or leave empty)
46 and run the "make-sri" action to compute the missing hashes.
48 This script runs in dry-run mode by default. Use --update to actually change,
49 remove, or add files to resources/lib/.
52 $this->addArg( 'action', 'One of "update", "verify" or "make-sri"', true );
53 $this->addArg( 'module', 'Name of a single module (Default: all)', false );
54 $this->addOption( 'verbose', 'Be verbose', false, false, 'v' );
56 // Use a directory in $IP instead of wfTempDir() because
57 // PHP's rename() does not work across file systems.
58 $this->tmpParentDir
= "{$IP}/resources/tmp";
61 public function execute() {
63 $this->action
= $this->getArg( 0 );
64 if ( !in_array( $this->action
, [ 'update', 'verify', 'make-sri' ] ) ) {
65 $this->fatalError( "Invalid action argument." );
68 $registry = $this->parseBasicYaml(
69 file_get_contents( __DIR__
. '/foreign-resources.yaml' )
71 $module = $this->getArg( 1, 'all' );
72 if ( $module === 'all' ) {
74 } elseif ( isset( $registry[ $module ] ) ) {
75 $modules = [ $module => $registry[ $module ] ];
77 $this->fatalError( 'Unknown module name.' );
80 foreach ( $modules as $moduleName => $info ) {
81 $this->verbose( "\n### {$moduleName}\n\n" );
82 $destDir = "{$IP}/resources/lib/$moduleName";
84 if ( $this->action
=== 'update' ) {
85 $this->output( "... updating '{$moduleName}'\n" );
86 $this->verbose( "... emptying /resources/lib/$moduleName\n" );
87 wfRecursiveRemoveDir( $destDir );
88 } elseif ( $this->action
=== 'verify' ) {
89 $this->output( "... verifying '{$moduleName}'\n" );
91 $this->output( "... checking '{$moduleName}'\n" );
94 $this->verbose( "... preparing {$this->tmpParentDir}\n" );
95 wfRecursiveRemoveDir( $this->tmpParentDir
);
96 if ( !wfMkdirParents( $this->tmpParentDir
) ) {
97 $this->fatalError( "Unable to create {$this->tmpParentDir}" );
100 if ( !isset( $info['type'] ) ) {
101 $this->fatalError( "Module '$moduleName' must have a 'type' key." );
103 switch ( $info['type'] ) {
105 $this->handleTypeTar( $moduleName, $destDir, $info );
108 $this->handleTypeFile( $moduleName, $destDir, $info );
111 $this->handleTypeMultiFile( $moduleName, $destDir, $info );
114 $this->fatalError( "Unknown type '{$info['type']}' for '$moduleName'" );
119 $this->output( "\nDone!\n" );
120 if ( $this->failAfterOutput
) {
121 // The verify mode should check all modules/files and fail after, not during.
126 private function fetch( $src, $integrity ) {
127 $data = Http
::get( $src, [ 'followRedirects' => false ] );
128 if ( $data === false ) {
129 $this->fatalError( "Failed to download resource at {$src}" );
131 $algo = $integrity === null ?
$this->defaultAlgo
: explode( '-', $integrity )[0];
132 $actualIntegrity = $algo . '-' . base64_encode( hash( $algo, $data, true ) );
133 if ( $integrity === $actualIntegrity ) {
134 $this->verbose( "... passed integrity check for {$src}\n" );
136 if ( $this->action
=== 'make-sri' ) {
137 $this->output( "Integrity for {$src}\n\tintegrity: ${actualIntegrity}\n" );
139 $this->fatalError( "Integrity check failed for {$src}\n" .
140 "\tExpected: {$integrity}\n" .
141 "\tActual: {$actualIntegrity}"
148 private function handleTypeFile( $moduleName, $destDir, array $info ) {
149 if ( !isset( $info['src'] ) ) {
150 $this->fatalError( "Module '$moduleName' must have a 'src' key." );
152 $data = $this->fetch( $info['src'], $info['integrity'] ??
null );
153 $dest = $info['dest'] ??
basename( $info['src'] );
154 $path = "$destDir/$dest";
155 if ( $this->action
=== 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
156 $this->fatalError( "File for '$moduleName' is different." );
157 } elseif ( $this->action
=== 'update' ) {
158 wfMkdirParents( $destDir );
159 file_put_contents( "$destDir/$dest", $data );
163 private function handleTypeMultiFile( $moduleName, $destDir, array $info ) {
164 if ( !isset( $info['files'] ) ) {
165 $this->fatalError( "Module '$moduleName' must have a 'files' key." );
167 foreach ( $info['files'] as $dest => $file ) {
168 if ( !isset( $file['src'] ) ) {
169 $this->fatalError( "Module '$moduleName' file '$dest' must have a 'src' key." );
171 $data = $this->fetch( $file['src'], $file['integrity'] ??
null );
172 $path = "$destDir/$dest";
173 if ( $this->action
=== 'verify' && sha1_file( $path ) !== sha1( $data ) ) {
174 $this->fatalError( "File '$dest' for '$moduleName' is different." );
175 } elseif ( $this->action
=== 'update' ) {
176 wfMkdirParents( $destDir );
177 file_put_contents( "$destDir/$dest", $data );
182 private function handleTypeTar( $moduleName, $destDir, array $info ) {
183 $info +
= [ 'src' => null, 'integrity' => null, 'dest' => null ];
184 if ( $info['src'] === null ) {
185 $this->fatalError( "Module '$moduleName' must have a 'src' key." );
187 // Download the resource to a temporary file and open it
188 $data = $this->fetch( $info['src'], $info['integrity' ] );
189 $tmpFile = "{$this->tmpParentDir}/$moduleName.tar";
190 $this->verbose( "... writing '$moduleName' src to $tmpFile\n" );
191 file_put_contents( $tmpFile, $data );
192 $p = new PharData( $tmpFile );
193 $tmpDir = "{$this->tmpParentDir}/$moduleName";
194 $p->extractTo( $tmpDir );
197 if ( $info['dest'] === null ) {
198 // Default: Replace the entire directory
199 $toCopy = [ $tmpDir => $destDir ];
201 // Expand and normalise the 'dest' entries
203 foreach ( $info['dest'] as $fromSubPath => $toSubPath ) {
204 // Use glob() to expand wildcards and check existence
205 $fromPaths = glob( "{$tmpDir}/{$fromSubPath}", GLOB_BRACE
);
207 $this->fatalError( "Path '$fromSubPath' of '$moduleName' not found." );
209 foreach ( $fromPaths as $fromPath ) {
210 $toCopy[$fromPath] = $toSubPath === null
211 ?
"$destDir/" . basename( $fromPath )
212 : "$destDir/$toSubPath/" . basename( $fromPath );
216 foreach ( $toCopy as $from => $to ) {
217 if ( $this->action
=== 'verify' ) {
218 $this->verbose( "... verifying $to\n" );
219 if ( is_dir( $from ) ) {
220 $rii = new RecursiveIteratorIterator( new RecursiveDirectoryIterator(
222 RecursiveDirectoryIterator
::SKIP_DOTS
224 foreach ( $rii as $file ) {
225 $remote = $file->getPathname();
226 $local = strtr( $remote, [ $from => $to ] );
227 if ( sha1_file( $remote ) !== sha1_file( $local ) ) {
228 $this->error( "File '$local' is different." );
229 $this->failAfterOutput
= true;
232 } elseif ( sha1_file( $from ) !== sha1_file( $to ) ) {
233 $this->error( "File '$to' is different." );
234 $this->failAfterOutput
= true;
236 } elseif ( $this->action
=== 'update' ) {
237 $this->verbose( "... moving $from to $to\n" );
238 wfMkdirParents( dirname( $to ) );
239 if ( !rename( $from, $to ) ) {
240 $this->fatalError( "Could not move $from to $to." );
246 private function verbose( $text ) {
247 if ( $this->hasOption( 'verbose' ) ) {
248 $this->output( $text );
252 private function cleanUp() {
253 wfRecursiveRemoveDir( $this->tmpParentDir
);
256 protected function fatalError( $msg, $exitCode = 1 ) {
258 parent
::fatalError( $msg, $exitCode );
264 * Supports only string or object values, and 2 spaces indentation.
266 * @todo Just ship symfony/yaml.
267 * @param string $input
270 private function parseBasicYaml( $input ) {
271 $lines = explode( "\n", $input );
275 foreach ( $lines as $i => $text ) {
277 $trimmed = ltrim( $text, ' ' );
278 if ( $trimmed === '' ||
$trimmed[0] === '#' ) {
281 $indent = strlen( $text ) - strlen( $trimmed );
282 if ( $indent %
2 !== 0 ) {
283 throw new Exception( __METHOD__
. ": Odd indentation on line $line." );
285 $depth = $indent === 0 ?
0 : ( $indent / 2 );
286 if ( $depth < $prev ) {
287 // Close previous branches we can't re-enter
288 array_splice( $stack, $depth +
1 );
290 if ( !array_key_exists( $depth, $stack ) ) {
291 throw new Exception( __METHOD__
. ": Too much indentation on line $line." );
293 if ( strpos( $trimmed, ':' ) === false ) {
294 throw new Exception( __METHOD__
. ": Missing colon on line $line." );
296 $dest =& $stack[ $depth ];
297 if ( $dest === null ) {
298 // Promote from null to object
301 list( $key, $val ) = explode( ':', $trimmed, 2 );
302 $val = ltrim( $val, ' ' );
305 $dest[ $key ] = $val;
307 // Add null (may become an object later)
310 $dest[ $key ] = &$val;
313 unset( $dest, $val );
319 $maintClass = ManageForeignResources
::class;
320 require_once RUN_MAINTENANCE_IF_MAIN
;